use std::path::PathBuf;

const HEADER: &str = r##"
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>ds v1.0</title>
    <style>
        * {
            padding: 0;
            margin: 0 auto;
        }
        #app {
            width: 960px;
            padding: 20px;
        }
        ul {
            font-size: 17px;
            list-style: none;
        }
        ul li {
            height: 27px;
            line-height: 27px;
        }
        ul li[type=disc] > a{
            color: darkgoldenrod;
        }
        a {
            text-decoration: none;
            color: dimgrey;
        }
        .file {
            background-image: url();
            background-repeat: no-repeat;
            background-size: 20px;
            background-position: 0 0;
            padding-left: 21px;
        }
        .folder {
            background-image: url();
            background-repeat: no-repeat;
            background-size: 20px;
            background-position: 0 0;
            padding-left: 21px;
        }
    </style>
</head>
<body>
"##;

const FOOTER: &str = r##"
</body>
</html>
"##;


pub fn pack_html(body: &str) -> String {
    let mut html = String::new();
    html.push_str(HEADER);
    html.push_str(body);
    html.push_str(FOOTER);
    return html;
}

pub fn pack_ul(dirs: Vec<PathBuf>) -> String{
    let mut code = String::new();
    code.push_str("<ul>");
    for d in dirs {
        if d.is_file() {
            code.push_str("<li class=\"file\">");
        } else {
            code.push_str("<li class=\"folder\">");
        }
        code.push_str("<a href=\"/");
        
        let file_name = d.file_name().unwrap().to_str().unwrap();
        let mut file_path = d.to_str().unwrap();
        file_path = &file_path[2..];
        code.push_str(file_path);
        code.push_str("\">");
        code.push_str(file_name);
        code.push_str("</a>");
        code.push_str("</li>");
    }
    code.push_str("</ul>");
    return code;
}